<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图1</title>
    <script src="./animate.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #CCC;
        }

        .banner {
            position: relative;
            margin: 100px auto;
            width: 400px;
            height: 200px;
            overflow: hidden;
            border-radius: 10px;
        }

        .banner ul {
            position: absolute;
            width: 2000px;
        }

        .banner ul li {
            float: left;
            list-style: none;
        }

        .banner ul li a {
            display: block;
            width: 400px;
            height: 200px;
        }

        .banner ul li a img {
            width: 100%;
            height: 100%;
        }

        .arr_left,
        .arr_right {
            display: none;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, .2);
            cursor: pointer;
        }

        .arr_left {
            float: left;
            left: 0;
            border-radius: 0 5px 5px 0;
        }

        .arr_right {
            float: right;
            right: 0;
            border-radius: 5px 0 0 5px;
        }

        .arr_left:hover,
        .arr_right:hover {
            background-color: rgba(0, 0, 0, .3);
        }

        .arrow img {
            width: 100%;
            height: 100%;
            opacity: .2;
        }

        .circle {
            position: absolute;
            left: 50%;
            bottom: 10px;
            transform: translateX(-50%);
            height: 13px;
            background-color: rgba(0, 0, 0, .2);
            border-radius: 7px;
            cursor: pointer;
        }

        .circle li {
            float: left;
            width: 7px;
            height: 7px;
            list-style: none;
            border-radius: 50%;
            margin: 3px;
        }

        .default {
            background-color: #fff;
        }

        .current {
            background-color: orange;
        }
    </style>
</head>

<body>
    <div class="banner">
        <ul id="oul">
            <li><a href="#"><img src="./images/b1.png" alt=""></a></li>
            <li><a href="#"><img src="./images/b4.png" alt=""></a></li>
            <li><a href="#"><img src="./images/b2.jpg" alt=""></a></li>
            <li><a href="#"><img src="./images/b3.jpg" alt=""></a></li>
        </ul>
        <div class="arrow">
            <div class="arr_left"><img src="./images/左箭头.png" alt=""></div>
            <div class="arr_right"><img src="./images/右箭头.png" alt=""></div>
        </div>
        <ol class="circle"></ol>
    </div>

    <script>
        var banner = document.getElementsByClassName('banner')[0];
        var arr_left = document.getElementsByClassName('arr_left')[0];
        var arr_right = document.getElementsByClassName('arr_right')[0];
        var circle = document.getElementsByClassName('circle')[0];
        var oul = banner.firstElementChild;

        // 轮播图的鼠标进入事件
        banner.addEventListener('mouseenter', function () {
            arr_left.style.display = 'block';
            arr_right.style.display = 'block';
            clearInterval(timer);
        })

        // 轮播图的鼠标离开事件
        banner.addEventListener('mouseleave', function () {
            arr_left.style.display = 'none';
            arr_right.style.display = 'none';
            timer = setInterval(() => {
                // 手动调用点击事件
                arr_right.click();
            }, 3000);
        })

        // 动态生成小圆圈，轮播图图片有多少张就生成多少个小圆圈
        for (let i = 0; i < oul.childElementCount; i++) {
            var li = document.createElement('li');
            li.setAttribute('data-index', i);
            li.className = 'default';
            // 给小圆圈绑定点击事件
            li.addEventListener('click', function () {
                changeDefault();
                this.className = 'current';
                // 移动的距离为索引号*图片的宽度
                var index = this.getAttribute('data-index');
                num = index;
                animate(oul, -banner.clientWidth * index);
            });
            circle.appendChild(li);
        }

        // 把第一个小圆圈变为黄色
        circle.firstElementChild.className = 'current';
        // circle.children[0].className = 'current';
        // 克隆第一个节点添加到最后一位
        oul.appendChild(oul.firstElementChild.cloneNode(true));

        // 存储点击的次数，3右加一，左减一
        var num = 0;
        // flag为节流阀
        var flag = true;
        // 右箭头点击事件
        arr_right.addEventListener('click', () => {
            if (flag) {
                // 关闭节流阀
                flag = false;
                // 到最后一张图片完全出现时迅速切换到第一张
                if (num == (oul.childElementCount - 1)) {
                    oul.style.left = 0;
                    num = 0;
                }
                num++;
                animate(oul, -banner.clientWidth * num, function () {
                    flag = true;
                });
                changeDefault();
                if (num == (oul.childElementCount - 1)) {
                    circle.children[0].className = 'current';
                } else {
                    circle.children[num].className = 'current';
                }
            }
        });

        // 左箭头点击事件
        arr_left.addEventListener('click', () => {
            if (flag) {
                flag = false;
                if (num == 0) {
                    num = oul.childElementCount - 1;
                    oul.style.left = -num * banner.clientWidth + 'px';
                }
                num--;
                animate(oul, -banner.clientWidth * num, function () {
                    flag = true;
                });
                changeDefault();
                circle.children[num].className = 'current';
                // console.log(num);
            };
        });

        // 自动播放功能
        arr_right.click();
        var timer = setInterval(() => {
            // 手动调用点击事件
            arr_right.click();
        }, 2400);

        // 将所有圆圈变为白色方法
        function changeDefault() {
            for (let j in circle.children) {
                circle.children[j].className = 'default';
            }
        }


    </script>
</body>

</html>